package com.beiing.tianshuai.tianshuai.mine.model;

import com.beiing.tianshuai.tianshuai.mine.entity.OrderUserAddressDataBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2018/4/10.
 * 用户地址信息请求
 */

public class OrderAddressModel implements OrderAddressModelImpl{

    private OrderAddressModel.OrderUserAddressDataResponseListener mListener;
    private static final String TAG = "OrderAddressModel";

    public OrderAddressModel(OrderAddressModel.OrderUserAddressDataResponseListener listener) {
        mListener = listener;
    }
    public interface OrderUserAddressDataResponseListener{
        void onGetDetailSuccess(OrderUserAddressDataBean bean);

        void onGetDetailError(Throwable throwable);

    }
    @Override
    public void getUserAddress(String user_id) {{
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("user_id", user_id)
                .build();
        Observable<OrderUserAddressDataBean> observable = DataManager.getOrderUserAddressData(body);
        Observable<OrderUserAddressDataBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observeOn.subscribe(new DisposableObserver<OrderUserAddressDataBean>() {
            @Override
            public void onNext(OrderUserAddressDataBean bean) {
                if (mListener != null) {
                    mListener.onGetDetailSuccess(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                if (mListener != null) {
                    LogUtils.e(e.getMessage(), TAG);
                    mListener.onGetDetailError(e);
                }
            }
            @Override
            public void onComplete() {

            }
        });
    }

    }
}
